package com.company.cloud.user.modules.mgr.controller;

import java.util.List;

import com.company.cloud.common.auth.annotation.StaffLogc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.auth.annotation.Authc;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.bean.LoginUserInfo;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.tree.CloudTree;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.user.modules.base.entity.DeptInfo;
import com.company.cloud.user.modules.base.service.DeptInfoService;
import com.company.cloud.user.modules.cache.models.DeptModel;
import com.company.cloud.user.modules.cache.utils.UserConvertUtils;
import com.company.cloud.user.modules.mgr.controller.req.DeptSavaParam;
import com.company.cloud.user.modules.mgr.controller.req.DeptTreeParam;
import com.company.cloud.user.modules.mgr.controller.req.DeptUpdateParam;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;

@Log4j2
@Authc
@RestController
@RequestMapping("/mgr/admin/dept")
@Api(tags="User_Mgr_部门管理")
public class DeptMgrController {

	@Autowired
	private DeptInfoService deptInfoService;
	
	@GetMapping("/queryTree")
	@ApiOperation(value="部门树形结构查询", response=DeptModel.class)
	public BaseResult queryTree(DeptTreeParam params) {
		LoginSimpleUserInfo userInfo=	AuthContextUtils.getUser();
		List<DeptInfo> depts = deptInfoService.list(new QueryWrapper<DeptInfo>().eq("company_no",userInfo.getCompanyNo()));
		CloudTree<DeptInfo, DeptModel> tree=new CloudTree<DeptInfo, DeptModel>() {
			@Override
			public boolean isTop(DeptModel entity) {
				if(entity.getParentId()==null || entity.getParentId()==0) {
					return true;
				}else {
					return false;
				}
			}
			@Override
			public boolean isParent(DeptModel parentObj, DeptModel childObj) {
				return parentObj.getId().intValue()==childObj.getParentId().intValue();
			}
			@Override
			public DeptModel convert(DeptInfo entity) {
				return UserConvertUtils.getDeptModel(entity);
			}
		};
		return BaseResult.ok(tree.tree(depts));
	}
	
	@GetMapping("/get")
	@ApiOperation(value="部门详情信息",response=DeptModel.class)
	public BaseResult get(@RequestParam("id") Integer id) {
		DeptInfo entity = deptInfoService.getById(id);
		DeptModel data= UserConvertUtils.getDeptModel(entity);
		return BaseResult.ok(data);
	}
	
	@PostMapping("/save")
	@StaffLogc(title = "User_Mgr_部门管理 、部门信息新增", action = "User_Mgr_部门管理、部门信息新增", actionAdo = "User_Mgr_部门管理")
	@ApiOperation(value="部门信息新增",response =DeptModel.class )
	public BaseResult save(@RequestBody DeptSavaParam params) {
		ValidatorUtils.validate(params);
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		DeptInfo entity = new DeptInfo();
		entity.setCompanyNo(userInfo.getCompanyNo());
		entity.setName(params.getName());
		entity.setType(params.getType());
		entity.setAddr(params.getAddr());
		entity.setLlType(params.getLlType());
		entity.setLongitude(params.getLongitude());
		entity.setLatitude(params.getLatitude());
		entity.setParentId(params.getParentId());
		entity.setDataAuth(params.getDataAuth());
		entity.setRemark(params.getRemark());
		entity.addCreateParam(userInfo);
		entity.addEditParam(userInfo);
		deptInfoService.save(entity);
		DeptModel data= UserConvertUtils.getDeptModel(entity);
		return BaseResult.ok(data);
	}
	
	@PostMapping("/update")
	@StaffLogc(title = "User_Mgr_部门管理 、部门信息修改", action = "User_Mgr_部门管理、部门信息修改", actionAdo = "User_Mgr_部门管理")
	@ApiOperation(value="部门信息修改",response = DeptModel.class)
	public BaseResult update(@RequestBody DeptUpdateParam params) {
		ValidatorUtils.validate(params);
		DeptInfo entity = deptInfoService.getById(params.getId());
		if (null == entity ) {
			throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
		}
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		entity.setName(params.getName());
		entity.setType(params.getType());
		entity.setAddr(params.getAddr());
		entity.setLlType(params.getLlType());
		entity.setLongitude(params.getLongitude());
		entity.setLatitude(params.getLatitude());
		entity.setParentId(params.getParentId());
		entity.setDataAuth(params.getDataAuth());
		entity.setRemark(params.getRemark());
		entity.addEditParam(userInfo);
		deptInfoService.updateById(entity);
		DeptModel data= UserConvertUtils.getDeptModel(entity);
		return BaseResult.ok(data);
	}
	
	@GetMapping("/delete")
	@StaffLogc(title = "User_Mgr_部门管理 、部门信息删除", action = "User_Mgr_部门管理、部门信息删除", actionAdo = "User_Mgr_部门管理")
	@ApiOperation(value="部门信息删除",response = BaseResult.class)
	public BaseResult delete(@RequestParam("id")Integer id) {
		DeptInfo entity = deptInfoService.getById(id);
		if (null == entity ) {
			throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
		}
		int childrenNum = deptInfoService.count(
				new QueryWrapper<DeptInfo>()
				.eq("company_no", entity.getCompanyNo()) 
				.eq("parent_id", id));
		if( childrenNum > 0 ) {
//			throw new BusinessException(UserErrorEnum.DEPT_HAS_CHILDREN);
			throw new BusinessException("该部门存在子节点");
		}
		deptInfoService.removeById(id);
		return BaseResult.ok();
	}

}
